﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Media;
using TomShane.Neoforce.Controls;

namespace Madrigal
{
    public enum LoginStep
    {
        Login,
        CheckUserPass,
        Connect,
        ServerList,
        CharacterList,
        WorldEntry,
    }

    class LoginScreen : GameScreen
    {

        Manager NeoManager { get; set; }
        ScreenManager screenManager { get; set; }

        Song bgm;
        Cue click = null;

        Window loginWindow;
        Window aboutWindow;
        Window creditWindow;
        Window connectBox;
        
        Label lblAccount;
        Label lblPassword;
        
        TextBox txtAccount;
        TextBox txtPassword;
        
        Button btnLogin;
        Button btnAbout;
        Button btnCredit;
        Button btnQuit;

        Texture2D aboutWindowTexture;

        public LoginScreen(ScreenManager sMgr, Manager manager)
        {
            screenManager = sMgr;
            bgm = screenManager.Game.Content.Load<Song>(@"Music\BgmDu1Mystery");
            aboutWindowTexture = screenManager.Game.Content.Load<Texture2D>(@"Textures\UI\WndAboutFlyff");
            MediaPlayer.IsRepeating = true;
            MediaPlayer.Play(bgm);
            NeoManager = manager;
            IsPopup = true;

            loginWindow = new Window(manager);
            loginWindow.Init();
            loginWindow.Resizable = false;
            loginWindow.Width = 275;
            loginWindow.Height = 300;
            loginWindow.Closed += new WindowClosedEventHandler(loginWindow_Closed);
            loginWindow.Center();

            lblAccount = new Label(manager);
            lblAccount.Init();
            lblAccount.SetPosition(loginWindow.ClientArea.Left + 10, loginWindow.ClientArea.Top + 5);

            lblPassword = new Label(manager);
            lblPassword.Init();
            lblPassword.SetPosition(loginWindow.ClientArea.Left + 10, (loginWindow.ClientArea.Top + 10) + 27);

            loginWindow.Text = "Login";
            lblAccount.Text = "Account";
            lblAccount.Alignment = Alignment.MiddleLeft;
            lblAccount.Parent = loginWindow;
            lblPassword.Text = "Password";
            lblPassword.Alignment = Alignment.MiddleLeft;
            lblPassword.Parent = loginWindow;

            txtAccount = new TextBox(manager);
            txtAccount.Init();
            txtAccount.Parent = loginWindow;
            txtAccount.SetPosition(lblAccount.Width + 32, lblAccount.Top);

            txtPassword = new TextBox(manager);
            txtPassword.Init();
            txtPassword.Parent = loginWindow;
            txtPassword.Mode = TextBoxMode.Password;
            txtPassword.SetPosition(lblPassword.Width + 32, lblPassword.Top);

            btnLogin = new Button(manager);
            btnLogin.Init();
            btnLogin.Parent = loginWindow;
            btnLogin.Text = "Login";
            btnLogin.SetPosition((lblPassword.Width + txtPassword.Width) / 2, lblPassword.Top + 48);
            btnLogin.Click +=new TomShane.Neoforce.Controls.EventHandler(btnLogin_Click);

            btnAbout = new Button(manager);
            btnAbout.Init();
            btnAbout.Parent = loginWindow;
            btnAbout.Text = "About Flyff";
            btnAbout.SetPosition(btnLogin.Left, (btnLogin.Top + btnLogin.Height) + 10);
            btnAbout.Click +=new TomShane.Neoforce.Controls.EventHandler(btnAbout_Click);

            btnCredit = new Button(manager);
            btnCredit.Init();
            btnCredit.Parent = loginWindow;
            btnCredit.Text = "Credits";
            btnCredit.Enabled = false;
            btnCredit.SetPosition(btnAbout.Left, (btnAbout.Top + btnAbout.Height) + 10);

            btnQuit = new Button(manager);
            btnQuit.Init();
            btnQuit.Parent = loginWindow;
            btnQuit.Text = "Exit";
            btnQuit.SetPosition(btnCredit.Left, (btnCredit.Top + btnCredit.Height) + 10);
            btnQuit.Click += new TomShane.Neoforce.Controls.EventHandler(btnQuit_Click);
            

            manager.Add(loginWindow);

            
        }

        void loginWindow_Closed(object sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            
            ScreenManager.Game.Exit();
        }

        void btnLogin_Click(object sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            screenManager.SoundBank.PlayCue("InfClick");
            CreateConnectBox(NeoManager);
        }

        void btnAbout_Click(object sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            screenManager.SoundBank.PlayCue("InfClick");
            CreateAboutWindow();
        }

        void btnQuit_Click(object sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            screenManager.SoundBank.PlayCue("InfClick");
            ScreenManager.Game.Exit();
        }

        void CreditWindow()
        {

        }

        #region Connect Box

        void CreateConnectBox(Manager manager)
        {
            TextBox tbConnectText;
            Button btnCancel;

            loginWindow.Focused = false;
            connectBox = new Window(manager);
            connectBox.Init();
            connectBox.SetPosition(loginWindow.Left + 24, loginWindow.Top + 72);
            connectBox.Text = "Connecting";
            connectBox.Visible = true;
            
            connectBox.Closed += new WindowClosedEventHandler(connectBox_Closed);
            tbConnectText = new TextBox(manager);
            tbConnectText.Init();
            tbConnectText.Text = "Connecting to Login Server...";
            tbConnectText.ReadOnly = true;
            tbConnectText.SetPosition(connectBox.ClientArea.Left + 32, connectBox.ClientArea.Top + 5);
            tbConnectText.Parent = connectBox;
            tbConnectText.ScrollBars = ScrollBars.None;

            btnCancel = new Button(manager);
            btnCancel.Init();
            btnCancel.Text = "Cancel";
            btnCancel.SetPosition(tbConnectText.Width / 2, tbConnectText.Top + 32);
            btnCancel.Click += new TomShane.Neoforce.Controls.EventHandler(btnCancel_Click);
            btnCancel.Parent = connectBox;

            connectBox.Width = tbConnectText.Width + 96;
            connectBox.Height = tbConnectText.Height + btnCancel.Height + 96;

            connectBox.Resizable = false;

            manager.Add(connectBox);
        }

        void btnCancel_Click(object sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            screenManager.SoundBank.PlayCue("InfClick");
            NeoManager.Remove(connectBox);
        }

        void connectBox_Closed(object sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            screenManager.SoundBank.PlayCue("InfClick");
            NeoManager.Remove(connectBox);
        }

        #endregion

        #region AboutFlyFF

        void CreateAboutWindow()
        {
            ImageBox ibAbout;
            loginWindow.Focused = false;
            aboutWindow = new Window(NeoManager);
            aboutWindow.Init();
            aboutWindow.Text = "About FlyFF";
            ibAbout = new ImageBox(NeoManager);
            ibAbout.Parent = aboutWindow;
            ibAbout.Image = aboutWindowTexture;
            
            ibAbout.Width = aboutWindowTexture.Width;
            ibAbout.Height = aboutWindowTexture.Height;
            ibAbout.Anchor = Anchors.Top;
            ibAbout.SetPosition(0, -24);

            aboutWindow.Closing +=new WindowClosingEventHandler(aboutWindow_Closing);

            NeoManager.Add(aboutWindow);
        }

        void aboutWindow_Closing(object sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            screenManager.SoundBank.PlayCue("InfClick");
            NeoManager.Remove(aboutWindow);
        }

        #endregion

    }
}
